Skip to content

Fix #10315: False positive: Dead catch#5100

Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-zq0cl0w
Open

Fix #10315: False positive: Dead catch#5100
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-zq0cl0w

Conversation

@phpstan-bot
Copy link
Collaborator

This PR fixes phpstan/phpstan#10315

Summary

  • Fix false positive "Dead catch" error when a class overrides a non-abstract trait method that has a @throws tag
  • Add ResolvedPhpDocBlock::withThrowsTag() to allow setting just the throws tag on a resolved PHPDoc block
  • Add regression test covering both scenarios: trait inherited through parent class and direct trait usage

Details

When implicitThrows: false is configured and a class overrides a non-abstract trait method that has a @throws tag, PHPStan incorrectly reports the catch as dead because the @throws tag is not inherited from the trait method.

The root cause is in PhpDocInheritanceResolver::resolvePhpDocForMethod(). The existing trait loop (lines 112-143) only processes abstract trait methods for full PHPDoc inheritance. This is correct for @return and @param tags — overriding a non-abstract trait method should not inherit those. However, @throws tags should still be inherited because the exception contract of the original trait method is still relevant when a catch block wraps a call in a different trait method on the same class.

The fix adds a targeted second pass after the abstract-only loop that specifically inherits @throws tags from non-abstract trait methods, without affecting @return/@param inheritance.

…od with @throws

When a class uses a trait with a non-abstract method that has a @throws tag,
and the class overrides that method, the @throws tag was not inherited. This
caused false positive "Dead catch" errors when implicitThrows was false, because
PHPStan did not recognize that the original trait method could throw the exception.

The existing trait PHPDoc inheritance in PhpDocInheritanceResolver only processed
abstract trait methods (to avoid inheriting @return/@param from non-abstract trait
methods). This fix adds a targeted second pass that inherits only @throws tags
from non-abstract trait methods when the overriding method has no @throws of its own.

Fixes phpstan/phpstan#10315
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant